package com.yyfax.pay.order.model.to.payin;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Ordering;
import com.yyfax.pay.common.enums.PayRspCodeEnum;
import com.yyfax.pay.common.enums.PayinBatchStepEnum;
import com.yyfax.pay.common.enums.PayoutStepEnum;
import com.yyfax.pay.order.model.po.PayinBatchOrderLogPO;
import com.yyfax.pay.order.model.po.PayoutOrderLogPO;

public class PayinBatchSnapshotVo extends PayinBatchVo{

	/**
	 */
	private static final long serialVersionUID = -6561699740072756554L;

	/** 订单是否生效 */
	private boolean isExpire;
	
	private List<PayinBatchOrderLogPO> orderLogPOs;
	
	
	public PayinBatchOrderLogPO getStep(PayinBatchStepEnum step) {
		if (orderLogPOs == null || orderLogPOs.isEmpty()) {
			return null;
		}

		for (PayinBatchOrderLogPO po : orderLogPOs) {
			if (step.isValue(po.getStep()) && PayRspCodeEnum.SUCCESS.isValue(po.getResultCode())) {
				return po;
			}
		}
		return null;
	}
	
	/**
	 * 获取最后一步日志（排除掉回复商户的日志）
	 * @return
	 */
	public PayinBatchOrderLogPO getOrderLastResultStep(){
		if(null == orderLogPOs){
			return null;
		}
		PayinBatchOrderLogPO lastResultPO = null;
		Collections.sort(orderLogPOs, new Comparator<PayinBatchOrderLogPO>() {
			public int compare(PayinBatchOrderLogPO object1, PayinBatchOrderLogPO object2) {
				return ComparisonChain.start()
			            .compare(object1.getStep(),object2.getStep(),Ordering.natural().reverse())
			            .result();
	        }  
		});
		for (PayinBatchOrderLogPO po : orderLogPOs) {
			if(PayinBatchStepEnum.STEP6_NOTIFY.isValue(po.getStep())
					|| PayinBatchStepEnum.STEP5_PAY_RECON_RESULT.isValue(po.getStep())){
				continue;
			}
			lastResultPO = po;
			break;
		}
		return lastResultPO;
	}
	
	public PayinOrderVO getPayinOrderVO(String payinOrderId) {
		if (getPayinOrderVOs() == null || getPayinOrderVOs().isEmpty()) {
			return null;
		}

		for (PayinOrderVO vo : getPayinOrderVOs()) {
			if (StringUtils.equals(vo.getPayinOrderId(), payinOrderId)) {
				return vo;
			}
		}

		return null;
	}

	public boolean isExpire() {
		return isExpire;
	}

	public void setExpire(boolean isExpire) {
		this.isExpire = isExpire;
	}

	public List<PayinBatchOrderLogPO> getOrderLogPOs() {
		return orderLogPOs;
	}

	public void setOrderLogPOs(List<PayinBatchOrderLogPO> orderLogPOs) {
		this.orderLogPOs = orderLogPOs;
	}
	
}
